package com.leyou.service;

import com.leyou.client.UserClient;
import com.leyou.common.enums.LyEnum;
import com.leyou.common.exception.LyException;
import com.leyou.entity.UserInfo;
import com.leyou.pojo.JwtProperties;
import com.leyou.user.pojo.User;
import com.leyou.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AutoService {
    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private UserClient client;

    public String login(String username, String password) {
        //校验用户名密码
        User user = client.queryUser(username, password);
        if(user==null){
            throw  new LyException(LyEnum.PASSWORD_ERROR);
        }else{
        //生成token
            String token = JwtUtils.generateToken(new UserInfo(user.getId(), user.getUsername()), jwtProperties.getPrivateKey(), jwtProperties.getExpire());
            return token;
        }
    }
}
